﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Business;
using System.Collections.ObjectModel;
using System.Collections;

namespace BitPortal.Admin.Feedback
{
    public partial class Default : Controls.BaseAdminPage
    {
        protected override void OnInit(EventArgs e)
        {
            PermissionBLL.CheckRoleThrowException(role: "CustomerView", useExceptionHandler: true);
            base.OnInit(e);
            (this.ReplyFeedback1 as Controls.BaseAdminControl).ReloadControls.Add(this.GridPanel1);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void LoadGridData()
        {
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");
            (this.GridPanel1.SelectionModel.Primary as RowSelectionModel).ClearSelections();
        }
        

        protected void btnView_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                long ID = Convert.ToInt64(sm.SelectedRows[0].RecordID);
                FeedbackDetail1.BindData(ID);

            }

        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            List<Models.Feedback> data;
            if (Session["FeedbackGridFlag"] != null && Session["FeedbackGridFlag"].Equals(false))
            {
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                data = bitEnt.Feedbacks.Where(ent => ent.IsDelete == true).ToList();
            }
            else
            {
                data = FeedbackBLL.GetAll().ToList();
            }
            FilterConditions fc = null;
            string s = e.Parameters[this.GridFilters1.ParamPrefix];
            if (!string.IsNullOrEmpty(s))
            {
                fc = new FilterConditions(s);
            }
            this.Store1.DataSource = ExtNet.GirdData.Filter<Models.Feedback>(data, fc, e);
        }

        protected void btnMarkAsReplied_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("CustomerView");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Feedback Feedback = bitEnt.Feedbacks.SingleOrDefault(ent => ent.FeedbackID == ID);
                    if (Feedback != null)
                        Feedback.IsReplied = true;
                }
                int record = bitEnt.SaveChanges();
                if (record > 0)
                {
                    ShowNotification("Cập nhật " + record + " liên hệ thành công!");
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnMarkAsUnreplied_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("CustomerView");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Feedback Feedback = bitEnt.Feedbacks.SingleOrDefault(ent => ent.FeedbackID == ID);
                    if (Feedback != null)
                        Feedback.IsReplied = false;
                }
                int record = bitEnt.SaveChanges();
                if (record > 0)
                {
                    ShowNotification("Cập nhật " + record + " liên hệ thành công!");
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnReplySelected_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("CustomerView");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Feedback Feedback = bitEnt.Feedbacks.SingleOrDefault(ent => ent.FeedbackID == ID);
                    if (Feedback != null)
                    {
                        ReplyFeedback1.ListFeedbacks.Add(Feedback);
                    }
                }
                ReplyFeedback1.BindData();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("CustomerView");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Feedback Feedback = bitEnt.Feedbacks.SingleOrDefault(ent => ent.FeedbackID == ID);
                    if (Feedback != null)
                    {
                        Feedback.IsDelete = true;
                    }
                }
                int record = bitEnt.SaveChanges();
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " liên hệ thành công!");
                    sm.ClearSelections();
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnTrash_Click(object sender, DirectEventArgs e)
        {
            if (Session["FeedbackGridFlag"] != null && Session["FeedbackGridFlag"].Equals(false))
            {
                Session["FeedbackGridFlag"] = true;
            }
            else
            {
                Session["FeedbackGridFlag"] = false;
            }
            LoadGridData();
        }

        protected void btnTrashEmpty_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("CustomerView");

                int record = 0;
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                foreach (BitPortal.Models.Feedback a in bitEnt.Feedbacks.Where(ent => ent.IsDelete == true).ToList())
                {
                    if (FeedbackBLL.DeletePermanently(a.FeedbackID) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " liên hệ trong thùng rác thành công!");
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnTrashDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("CustomerView");

                int record = 0;
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    if (FeedbackBLL.DeletePermanently(Convert.ToInt64(row.RecordID)) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " liên hệ trong thùng rác thành công!");
                    sm.ClearSelections();
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnUnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("CustomerView");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Feedback Feedback = bitEnt.Feedbacks.SingleOrDefault(ent => ent.FeedbackID == ID);
                    if (Feedback != null)
                        Feedback.IsDelete = false;
                }
                int record = bitEnt.SaveChanges();
                if (record > 0)
                {
                    ShowNotification("Bỏ xóa " + record + " liên hệ trong thùng rác thành công!");
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }
    }
}